/*
 * telaLogin.java
 *
 * Created on 10 de Dezembro de 2006, 04:00
 */

package magchess10.ClassesIU;



import magchess10.rn.bootStrap.Login;
import magchess10.rn.controle.ControleNaoJogo;

/**
 *
 * @author  User
 */
public class telaLogin extends javax.swing.JFrame {
    private ControleNaoJogo c_controleNaoJogo;
    /** Creates new form telaLogin */
    public telaLogin(ControleNaoJogo p_controleNaoJogo) {
        initComponents();
        setControleNaoJogo(p_controleNaoJogo);
        lblInvalido.setVisible(false);
        
    }
    
    protected void setControleNaoJogo(ControleNaoJogo p_controleNaoJogo) {
        try {
            c_controleNaoJogo = p_controleNaoJogo;
        } catch (Exception e){System.out.println(e.toString());}
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        txtApelido = new javax.swing.JTextField();
        btnCriarConta = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnValidarLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtSenha = new javax.swing.JPasswordField();
        lblInvalido = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MagChess 1.0 Login");
        setName("telaLogin");
        setResizable(false);

        btnCriarConta.setText("Criar conta");
        btnCriarConta.setToolTipText("Clique aqui para criar nova conta!");
        btnCriarConta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCriarConta_Click(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnValidarLogin.setText("Validar login");
        btnValidarLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValidarLoginActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
        jLabel1.setLabelFor(txtApelido);
        jLabel1.setText("Apelido:");

        jLabel2.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
        jLabel2.setText("Senha:");

        jLabel3.setText("Ainda n\u00e3o possui cadastro?");

        lblInvalido.setFont(new java.awt.Font("MS Sans Serif", 0, 14));
        lblInvalido.setForeground(new java.awt.Color(255, 51, 51));
        lblInvalido.setText("Login ou senha inv\u00e1lidos");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 11, Short.MAX_VALUE)
                                .add(btnCriarConta))
                            .add(layout.createSequentialGroup()
                                .add(btnValidarLogin)
                                .add(12, 12, 12)
                                .add(btnCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel1)
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(txtSenha, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                    .add(txtApelido, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))))
                        .add(30, 30, 30))
                    .add(layout.createSequentialGroup()
                        .add(lblInvalido)
                        .addContainerGap(105, Short.MAX_VALUE))))
        );

        layout.linkSize(new java.awt.Component[] {btnCancelar, btnValidarLogin}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(btnCriarConta))
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtApelido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(15, 15, 15)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtSenha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(19, 19, 19)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnValidarLogin)
                    .add(btnCancelar))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblInvalido)
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {txtApelido, txtSenha}, org.jdesktop.layout.GroupLayout.VERTICAL);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnValidarLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValidarLoginActionPerformed
        if (ValidarLogin()) {
            definirNomeJogador();
            registrarCliente ();
            RedirecionarTelaMesa();
        } else
        {
            lblInvalido.setVisible(true);
            limparCampos();
        }
    }//GEN-LAST:event_btnValidarLoginActionPerformed
    
    private void definirNomeJogador() {
        getControleNaoJogo().setNomeJogador(txtApelido.getText());
    }
    
    private void limparCampos()
    {
        txtApelido.setText("");
        txtSenha.setText("");
    }
    
    private void registrarCliente ()
    {
        c_controleNaoJogo.registrarCliente();
    }
    
    protected ControleNaoJogo getControleNaoJogo() {
        return c_controleNaoJogo;
    }
    
    
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed
    
    private void btnCriarConta_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCriarConta_Click
        RedirecionarTelaCadastro();
        
    }//GEN-LAST:event_btnCriarConta_Click
    
    private void RedirecionarTelaMesa() {
        
        getControleNaoJogo().RedirecionarTelaMesa();
    }
    
    private void RedirecionarTelaCadastro() {
        getControleNaoJogo().RedirecionarTelaCadastro();
    }
    
    private boolean ValidarLogin() {
        Login l_Login = new Login(txtApelido.getText(),String.valueOf(txtSenha.getPassword()));
        if (l_Login.validarDados())
            try {
                return c_controleNaoJogo.pedirConsultaBD(l_Login);
            } catch (Exception e){System.out.println(e.toString());}
        return false;
    }
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCriarConta;
    private javax.swing.JButton btnValidarLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblInvalido;
    private javax.swing.JTextField txtApelido;
    private javax.swing.JPasswordField txtSenha;
    // End of variables declaration//GEN-END:variables
}

